import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:moose_app/core/model/chat_message_model.dart';
import 'package:moose_app/ui/values/values.dart';
import 'package:moose_app/ui/widgets/chat/chat_input_operator.dart';
import 'package:moose_app/ui/widgets/chat/chat_message_item.dart';
import 'package:moose_app/ui/widgets/scroll/scroller_configuration_wrapper.dart';

class ATHChatBody extends StatefulWidget {
  @override
  _ATHChatBodyState createState() => _ATHChatBodyState();
}

class _ATHChatBodyState extends State<ATHChatBody> {
  List<ATHChatMessageModel> _chatMessages = [];

  TextEditingController _messageInputController;

  ScrollController _messageScrollController;

  final FocusNode _chatInputFocusNode = FocusNode();

  @override
  void initState() {
    super.initState();

    _chatMessages
      ..add(ATHChatMessageModel(
        ownerType: ATHOwnerType.sender,
        messageType: ATHChatMessageType.text,
        message: "今天你吃饭了吗？\n今天你吃饭了吗？",
        userName: "江景",
      ))
      ..add(ATHChatMessageModel(
        ownerType: ATHOwnerType.receiver,
        messageType: ATHChatMessageType.text,
        message: "吃了",
        userName: "测试",
      ))
      ..add(ATHChatMessageModel(
        ownerType: ATHOwnerType.sender,
        messageType: ATHChatMessageType.text,
        message: "今天你吃饭了吗？",
        userName: "江景",
      ))
      ..add(ATHChatMessageModel(
        ownerType: ATHOwnerType.receiver,
        messageType: ATHChatMessageType.text,
        message: "吃了",
        userName: "测试",
      ))
      ..add(ATHChatMessageModel(
        ownerType: ATHOwnerType.sender,
        messageType: ATHChatMessageType.image,
        imageUrl: "assets/images/qrcode_430.jpg",
        userName: "江景",
      ));

    _messageInputController = TextEditingController();

    _messageScrollController = ScrollController();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: Container(
            padding:
                EdgeInsets.symmetric(horizontal: kSize24, vertical: kSize20),
            child: ATHScrollerConfigurationWrapper(
              child: ListView.builder(
                  controller: _messageScrollController,
                  itemCount: _chatMessages.length,
                  itemBuilder: (context, index) {
                    ATHChatMessageModel messageModel = _chatMessages[index];
                    return ATHChatMessageItem(messageModel: messageModel);
                  }),
            ),
          ),
        ),
        ATHChatInputOperator(
          focusNode: _chatInputFocusNode,
          chatInputController: _messageInputController,
          press: _handleMessageInputSubmit,
        )
      ],
    );
  }

  void _handleMessageInputSubmit(String value) {
    /// value is empty hide keyboard
    if (value.isEmpty) {
      return;
    }

    ATHChatMessageModel model = ATHChatMessageModel(
        ownerType: ATHOwnerType.sender,
        messageType: ATHChatMessageType.text,
        message: value,
        userName: "江景",
        userAvatar: 'assets/images/avatar_boy.png');
    _chatMessages..add(model);
    this.setState(() {
      _chatMessages = _chatMessages;
    });
    _messageInputController.clear();
    Timer(
        Duration(milliseconds: 100),
        () => _messageScrollController
            .jumpTo(_messageScrollController.position.maxScrollExtent));
  }
}
